/*
 * @Author: szx
 * @Date: 2023-03-31 13:54:27
 * @LastEditTime: 2023-04-01 16:06:14
 * @Description:
 * @FilePath: \learn\fullstackopen\bloglist-part5\src\services\blogs.js
 */
import axios from 'axios'
const baseUrl = '/api/blogs'

let token = null

const getAll = async () => {
    const request = await axios.get(baseUrl);
    return request.data;
}
const setToken = newToken => {
    token = `bearer ${newToken}`
}

const create = async newObject => {
    const config = {
        headers: { Authorization: token },
    }

    const response = await axios.post(baseUrl, newObject, config)
    return response.data
}


const update = async (id, newObject) => {
    const config = {
        headers: { Authorization: token },
    }
    const response = await axios.put(`${baseUrl}/${id}`, newObject, config)
    console.log(response)
    return response.data
}

const deleteBlog = async (id) => {
    const config = {
        headers: { Authorization: token },
    }
    const response = await axios.delete(`${baseUrl}/${id}`, config)
    return response.data
}


export { getAll, create, update, setToken, deleteBlog }
